home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 49 / Amiga Format CD49 (2000-01-17)(Future Publishing)(GB)(Track 1 of 3)[!][issue 2000-02].iso / -in_the_mag- / program_perfection / test.c < prev    next >
C/C++ Source or Header  |  1999-12-08  |  2KB  |  80 lines

  1. /*
  2.  * test.c
  3.  *
  4.  * A quick and dirty test for text class
  5.  *
  6.  * Pass <filename> as an argument to dump file to console.
  7.  *
  8.  * $Id :$
  9.  * $Log:$
  10.  *
  11.  */
  12.  
  13.  
  14. #include "defs.h"
  15. #include "lines.h"
  16. #include "text.h"
  17. #include "memory.h"
  18. #include "errors.h"
  19. #include <dos/dos.h>
  20. #include <proto/dos.h>
  21.  
  22.  
  23. int
  24. main( int argc, char **argv )
  25. {
  26.  
  27.     ERROR_TYPE err = NULL;
  28.  
  29.     if( argc > 1 )
  30.     {
  31.         BPTR file;
  32.  
  33.         /* open file */
  34.         if( file = Open( argv[1], MODE_OLDFILE ) )
  35.         {
  36.             LONG file_length;
  37.             APTR file_buf;
  38.  
  39.             /* find file size */
  40.             Seek( file, 0L, OFFSET_END );
  41.             file_length = Seek( file, 0L, OFFSET_BEGINNING );
  42.  
  43.             Printf( "size: %ld\n", file_length );
  44.             /* allocate buffer */
  45.             if( file_buf = ( Memory_Alloc( file_length ) ) )
  46.             {
  47.                 if( Read( file, file_buf, file_length ) == file_length )
  48.                 {
  49.                     /* dump it to the console */
  50.                     TEXT_PTR t;
  51.                     LINE_PTR l;
  52.  
  53.                     t = Text_New( file_buf, &err );
  54.  
  55.                     FPrintf( Output(), "Longest line:%ld\n\n", Text_GetMaxWidth( t ) );
  56.  
  57.                     /* walk this list */
  58.                     for( l = Text_GetTopLine( t ); l; l = Text_GetNextLine( t, l ) )
  59.                     {
  60.                         Line_FPuts( Output(), l );
  61.                     }
  62.  
  63.                 }
  64.  
  65.                 /* done with buffer */
  66.                 Memory_Free( file_buf, file_length );
  67.             }
  68.  
  69.             /* done with file */
  70.             Close( file );
  71.         }
  72.         else
  73.             MyExit( "Couldn't open file '%s'", argv[1] );
  74.  
  75.     }
  76.  
  77.     MyExit( NULL );
  78. }
  79.  
  80.